const path = require('path');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin')
const externals = {
    //生产环境不进行打包
}
const plugins = [
    new CompressionWebpackPlugin({
        filename: '[path].gz[query]',
        algorithm: 'gzip',
        test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$', ),
        threshold: 10240,
        minRatio: 0.8,
    }),
];
// 移除log debugger
const optimization = {
    minimizer: [
        new TerserPlugin({
            terserOptions: {
                ecma: undefined,
                warnings: false,
                parse: {},
                compress: {
                    drop_console: true,
                    drop_debugger: true,
                    pure_funcs: ['console.log']
                }
            },
        }),
    ]
}
module.exports = {
    chainWebpack(config) {

        config.module
            .rule('swf')
            .test(/\.swf$/)
            .use('url-loader')
            .loader('url-loader')
            .options({

                limit: 1024,
                name: 'file/[path][name].[hash:7].[ext]'

            })

            .end()

    },
    // css: {
    //     loaderOptions: {
    //         css: {},
    //         postcss: {
    //             plugins: [
    //                 require('postcss-px2rem')({
    //                     remUnit: 37.5
    //                 })
    //             ]
    //         }
    //     }
    // },
    pages: {
        index: {
            entry: 'src/main.js',
            template: 'public/index.html',
            filename: 'index.html',
            chunks: ['chunk-vendors', 'chunk-common', 'index']
        }
    },
    productionSourceMap: false,
    pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'less',
            patterns: [path.resolve(__dirname, 'src/theme/variable.less')]
        }
    },
    configureWebpack: config => {
        if (process.env.NODE_ENV !== 'development') {
            config.plugins = [...config.plugins, ...plugins]
            config.externals = externals
            config.optimization = optimization
        }
    },
    devServer: {
        open: process.platform === 'darwin',
        host: '0.0.0.0',
        https: false,
        hotOnly: false,
        proxy: {
            '/api/*': {
                target: 'http://192.168.2.56:6060', // 开发环境地址
                ws: true, // 是否代理 WebSockets
                changeOrigin: true, // 跨域配置
                pathRewrite: {
                    '^/api': ''
                }
            },
        }
    }
};